package com.guit.hrms.service.impl;

import com.guit.hrms.mapper.AcademyMapper;
import com.guit.hrms.mapper.AnnouncementMapper;
import com.guit.hrms.mapper.DepartmentMapper;
import com.guit.hrms.mapper.OtherDeptMapper;
import com.guit.hrms.po.Announcement;
import com.guit.hrms.service.AnnouncementService;
import com.guit.hrms.vo.department.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AnnouncementImpl implements AnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private OtherDeptMapper otherDeptMapper;

    /**
     * 保存公告
     * @param announcement 公告信息
     */
    @Override
    public Integer saveAnnouncements(Announcement announcement) {
        Integer deptId = announcement.getDeptId();
        Integer deptType = announcement.getDeptType();
        if (deptType == 1){
            if (departmentMapper.selectDepartmentById(deptId) == null){
                throw new RuntimeException("部门不存在");
            }
        } else if (deptType == 2){
            if (academyMapper.getAcademyById(deptId) == null){
                throw new RuntimeException("学院不存在");
            }
        } else if (deptType == 3){
            if (otherDeptMapper.getOtherDeptById(deptId) == null){
                throw new RuntimeException("其他部门不存在");
            }
        } else {
            throw new RuntimeException("部门类型错误");
        }
        String title = announcement.getTitle();
        String content = announcement.getContent();
        LocalDateTime publishTime = announcement.getPublishTime();
        System.out.println(announcement.toString());
        return announcementMapper.saveAnnouncements(deptId, deptType,title,content,publishTime);
    }

    /**
     * 查看公告,若deptId和deptType为空则查询所有部门的公告
     * @param deptId   部门id
     * @param deptType 部门类型
     */
    @Override
    public List<Announcement> viewAnnouncements(Integer deptId, Integer deptType) {

        List<Announcement> announcementList = announcementMapper.getByIdAndType(deptId, deptType);
        announcementList.forEach(announcement -> {
            try {
                if (announcement.getDeptType() == 1) {
                    announcement.setDeptName(departmentMapper.selectDepartmentById(announcement.getDeptId()).getDeptName());
                } else if (announcement.getDeptType() == 2) {
                    announcement.setDeptName(academyMapper.getAcademyById(announcement.getDeptId()).getAcademyName());
                } else if (announcement.getDeptType() == 3) {
                    announcement.setDeptName(otherDeptMapper.getOtherDeptById(announcement.getDeptId()).getOtherDeptName());
                } else {
                    announcement.setDeptName(null);
                }
            } catch (Exception e) {
                announcement.setDeptName(null);
                e.getMessage();
            }
        });

        return announcementList;
    }

    /**
     * 更新公告
     * @param announcement 公告信息
     * @return 更新结果
     */
    @Override
    public Integer updateAnnouncements(Announcement announcement) {
        Integer id = announcement.getId();
        Integer deptId = announcement.getDeptId();
        Integer deptType = announcement.getDeptType();
        if (deptType == 1){
            if (departmentMapper.selectDepartmentById(deptId) == null){
                throw new RuntimeException("部门不存在");
            }
        } else if (deptType == 2){
            if (academyMapper.getAcademyById(deptId) == null){
                throw new RuntimeException("学院不存在");
            }
        } else if (deptType == 3){
            if (otherDeptMapper.getOtherDeptById(deptId) == null){
                throw new RuntimeException("其他部门不存在");
            }
        } else {
            throw new RuntimeException("部门类型错误");
        }
        String title = announcement.getTitle();
        String content = announcement.getContent();
        LocalDateTime publishTime = announcement.getPublishTime();
        System.out.println(announcement.toString());
        return announcementMapper.updateAnnouncements(id,deptId,deptType,title,content,publishTime);
    }

    /**
     * 删除公告
     * @param ids 公告id列表
     * @return 删除结果
     */
    @Override
    public Integer deleteAnnouncements(List<Integer> ids) {
        return announcementMapper.deleteAnnouncements(ids);
    }
}
